<template>
  <div class="login-container">
    <div class="header">
      <div class="title">高校论坛</div>
    </div>
    <!-- 中间 -->
    <div class="center">
      
      <el-form ref="loginForm" size="small" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
        <div style="text-align:center;height:60px;line-height:60px;font-weight:bold">用户名密码登录</div>
        <el-form-item prop="username">
          <span class="svg-container"> <svg-icon icon-class="user" /> </span>
          <el-input v-model="loginForm.username" placeholder="Username" type="text" ref="username" />
        </el-form-item>
        <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="Password"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
        </el-tooltip>
        <div>
          <el-button :loading="loading"  style="width:100%;" @click.native.prevent="handleLogin">登录</el-button>
        </div>
        <!-- <div>
          <span>没有账号？</span>
          <el-button type="text" @click="toRegisterHandler">注册</el-button>
        </div> -->
      </el-form>
      <!-- 装饰图 -->
      <div class="deco" style="margin-left:50px;">
        <img src="./images/logo_02.png" alt="">
      </div>

       <!-- 模态框 -->
      <!-- <el-dialog title="用户注册" :visible.sync="visible" width="40%" @close="clearValidate" class="customer_modal">
        <div>
          <el-form :model="form" ref="registerForm" :rules="rules" label-width="80px">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="realname">
              <el-input v-model="form.realname"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="form.password"></el-input>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button size="small" @click="visible = false">取消</el-button>
          <el-button size="small" type="primary" @click="submitHandler">确定</el-button>
        </span>
      </el-dialog> -->
      <!-- 模态框 -->
    </div>
  </div>
</template>

<script>
import {post} from '@/utils/request'
export default {
  name: 'Login',
  data() {
    
    const validateUsername = (rule, value, callback) => {
      callback()
    }
    const validatePassword = (rule, value, callback) => {
      callback()
    }
    return {
      visible:false,
      form:{},
      rules:{
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        realname: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    // 提交注册
    submitHandler(){
      this.$refs['registerForm'].validate((valid) => {
        if (valid) {
          let url = '/baseUser/register'
          post(url,this.form).then(response => {
            this.$message({type:"success",message:response.message})
            this.visible = false;
          })
        } else {
          return false;
        }
      });
    },
    // 重置表单校验
    clearValidate() {
      this.$refs['registerForm'].clearValidate();
    },
    // 去保存
    toRegisterHandler(){
      this.form = {};
      this.visible = true;
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .header {
    line-height: 100px;
    height: 100px;
    padding:0 100px;
    .title {
      font-size:26px;
    }
  }
  .el-input {
    display: inline-block;
    height: 36px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #333;//$light_gray;
      height: 36px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#409eff;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container .center{
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  padding: 80px 100px 80px 150px;
  position: relative;
  .deco {
    height:300px;

    img {
      height:100%
    }
  }

  .login-form {
    float:right;
    width: 360px;
    height: 300px;
    padding: 20px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    background-color: #fff;
    border-radius: 5px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: 900;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
